<?php

require_once ( LIB_DIR.'search/moviesearch.php');
/**
 * detail page for each item
 *
 * @author Jensen
 */
class item_new extends AbstractAction {

    public function run() {
        global $TPL;
        $itemId = intval($_GET['id']);
        $filePath = CRAWLER_DATA.floor( $itemId/100 )."/".$itemId.'.php';
        
        if(file_exists( $filePath )){
            $movieDao = $this->getDao("MovieDAO");
            $itemInfoDB = $movieDao->GetMovieById($itemId);
            $itemInfo = require $filePath;
            
            $itemInfo['important_short'] = array();
            foreach( $itemInfo['intro'] as $key=>$value ){
                switch( $key ){
                    case "地区":
                        $itemInfo['important_short']['location']=$value;
                        break;
                    case "英文":
                        $itemInfo['important_short']['englishname']=$value;
                        break;
                    case "别名":
                        $itemInfo['important_short']['othername']=$value;
                        break;
                    case "主演":
                        $itemInfo['important_short']['actors']=$value;
                        break;
                }
            }
            
            // remove content before 展开全文
            $pos = strpos($itemInfo['intro']['简介'],'[展开全文]');
            if( $pos !== false ){
                $itemInfo['intro']['简介'] = substr($itemInfo['intro']['简介'], strpos($itemInfo['intro']['简介'],'[展开全文]')+14);
            }
            $itemInfo['intro']['简介'] = trim($itemInfo['intro']['简介']);
            
            
            if( !empty($itemInfo['intro']['简介']) ){
                if(mb_strlen( $itemInfo['intro']['简介'], 'UTF-8') > 200 ){
                    $itemInfo['full_intro'] = $itemInfo['intro']['简介'];
                    $itemInfo['intro']['简介'] = mb_substr($itemInfo['intro']['简介'], 0, 200, 'UTF-8');
                    $itemInfo['short_intro'] = $itemInfo['intro']['简介'];
                }
            }
            
            // process movie type
            $itemInfo['intro']['类型'] = array_map('trim', explode("/", $itemInfo['intro']['类型'] ));
            $itemInfo['category']['types'] = $itemInfo['intro']['类型'];
            if( !empty($itemInfo['intro']['主演']) ){
                $itemInfo['intro']['主演'] = array_map('trim', explode("/", $itemInfo['intro']['主演'] ));
            }
            if( !empty($itemInfo['intro']['导演']) ){
                $itemInfo['intro']['导演'] = array_map('trim', explode("/", $itemInfo['intro']['导演'] ));
            }
            if( !empty($itemInfo['intro']['编剧']) ){
                $itemInfo['intro']['编剧'] = array_map('trim', explode("/", $itemInfo['intro']['编剧'] ));
            }
            if( !empty($itemInfo['intro']['英文']) ){
				$TPL->assign('item_englishname', $itemInfo['intro']['英文']);
			}
            
            // reorder download
            $arrHead = array();
            $arrTail = array();
            foreach( $itemInfo['download'] as $key=>$download){
                if( $key >= 100 ){
                    $arrTail["$key"] = $download;
                }
                else{
                    $arrHead["$key"] = $download;
                }
            }
            $itemInfo['download'] = $arrHead + $arrTail;
            // get related items
            $relatedItems = $this->getRelatedMovies( $itemId, $itemInfo['intro']['类型'] );
            $relatedHotItems = $this->getHotMovies( $itemId, $itemInfo['intro']['类型'] );
            
            $itemInfo['largeImgUrl'] = $this->getLargeImgUrl( $itemInfo['imgUrl'] );
            
            // get all related image large url
            $count = 0;
            foreach( $itemInfo['image'] as $imageUrl ){
                if( $count < 3 ){
                    $imageUrlLarge = $this->getLargeImgUrl( $imageUrl );
                    $itemInfo['relatedimage'][] = array( 'small' => $imageUrl, 'large' => $imageUrlLarge );
                }
                else{
                    break;
                }
                $count++;
            }
            
            $itemInfo = array_merge( $itemInfoDB, $itemInfo );
            // update item viewcount
            $movieDao->UpdateItemViewCount($itemId);
            
            $TPL->assign('relatedItems', $relatedItems);
            $TPL->assign('relatedHotItems', $relatedHotItems);
            $TPL->assign('item', $itemInfo);
            $TPL->display('theme_v2/item.tpl');
        }
        else{
            echo "未找到相应资源, 回到首页......";
            js_redirect('index.php');
        }
    }
    
    /**
     * 获得相关资源
     * 
     * @param type $currentId
     * @param type $arrCategories
     * @return type
     */
    private function getRelatedMovies( $currentId, $arrCategories ){
        $index = 0;
        $keyword = "";
        foreach( $arrCategories as $cate ){
            if( $index == 3 ){
                break;
            }
            
            $keyword = $keyword." ".$cate;
            
            $index ++;
        }
        if( debug ){
            $search = new moviesearch( "42.121.129.20", 9312 );
        }
        else{
            $search = new moviesearch();
        }

        $keyword = trim($keyword);
        if( empty( $keyword ) ) return array();
        
        $result = $search->Query( "@type ".trim($keyword) );
        
        if( $result['total'] > 1 ) {
            // remove self
            foreach( $result['matchIds'] as $key=>$id ){
                if( $id == $currentId ){
                    unset( $result['matchIds'][$key] );
                }
            }
            
            //!!!!shit here, need to sort the result with extra sql statement : ORDER BY FIELD(id, ".$strIds.")
            //and then do sort here
            $movieDao = $this->getDao("MovieDAO");
            $dbRes = $movieDao->GetMovieInfoByIdList( $result['matchIds'], RELATED_PERPAGE );
            return $dbRes;
        }
        else{
            return array();
        }
    }

    /**
     * 获得最热资源
     * 
     * @param type $currentId
     * @param type $arrCategories
     * @return type
     */
    private function getHotMovies( $currentId, $arrCategories ){
        $index = 0;
        $keyword = "";
        foreach( $arrCategories as $cate ){
            if( $index == 3 ){
                break;
            }
            
            $keyword = $keyword." ".$cate;
            
            $index ++;
        }
        if( debug ){
            $search = new moviesearch( "42.121.129.20", 9312 );
        }
        else{
            $search = new moviesearch();
        }

        $keyword = trim($keyword);
        if( empty( $keyword ) ) return array();
        
        $result = $search->SortQuery( "@type ".trim($keyword), "viewcount", 1, true);
        
        if( $result['total'] > 1 ) {
            // remove self
            foreach( $result['matchIds'] as $key=>$id ){
                if( $id == $currentId ){
                    unset( $result['matchIds'][$key] );
                }
            }
            
            //!!!!shit here, need to sort the result with extra sql statement : ORDER BY FIELD(id, ".$strIds.")
            //and then do sort here
            $movieDao = $this->getDao("MovieDAO");
            $dbRes = $movieDao->GetMovieInfoByIdList( $result['matchIds'], RELATED_PERPAGE );
            return $dbRes;
        }
        else{
            return array();
        }
    }
}

?>
